<?php
class Admin_IndexController extends Zend_Controller_Action{
		
	public function init(){
		//parent::init();
		
	}
    
    public function preDispatch() 
    {
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            if ($this->_request->getActionName() != 'login') {
                $this->_redirect('/user/login');
            }
        }
    }
    
	public function indexAction()
    {

           
	}
	
    public function createAction()
    {
         try {
                $request = $this->getRequest();
                if($request->isPost())
                {
                    $user = new Application_Model_User();
                    $user->setUsername($request->get('txtuname'));
                    $user->setEmail($request->get('txtEmail'));
                    $user->setPassword(md5($request->get('txtPassword')));
                    $user->setStatus(1);
                    $user->save();
                    $this->_redirect('/admin');
                    }
            }
            catch(exception $ex)
            {
                print_r($ex);
            }
    } 
	
	public function viewAction(){
		
	}
    public function editAction(){
        try 
        {
            
            $request = $this->getRequest();
            if($request->get('id'))
            {
                $user = new Application_Model_User();
                $where['id = ?'] = $request->get('id');                
                $users = $user->fetchAll($where);
                $user = $users[0];
                $this->view->users = $users;
                
            }
            if($request->isPost())
            {
            	$user2 = new Application_Model_User();
                $user2->setUsername($request->get('txtUname'));
                $user2->setEmail($request->get('txtEmail'));
                $user2->setStatus($request->get('txtStatus'));
                if($request->get('txtRole'))
                	$user2->setRole($request->get('txtRole'));                
                $user2->save($user);                
                $this->_redirect('/admin/index/userlist');
            }                           
        }
        catch(exception $ex)
        {
            print_r($ex);
        }
    }
    public function userlistAction(){
        try {
                $user = new Application_Model_DbTable_User();
                $this->view->user = $user->fetchAll();
                
        }
        catch(exception $ex)
        {
            print_r($ex);
        }
    }
    
    public function usernameAction()
    {          
        $user_mapper = new Application_Model_User();   
        if($params=$this->getRequest()->getParams())
        {
            $user =$params['txtuname'];
            $users = $user_mapper->fetchAll(array('username = ?'=>$user)); 
            if(count($users)>0)
            {
                $this->view->userExist ='This username has already exist. Please register by another username';
            } 
            else
            {
                $this->view->userExist ='This email is available';
            }
        }
    }
	


}